@extends('Admin.Layout.main')
@section('privateheader')
    <style>
        #check_node_data{
            float:left;
            overflow-y: scroll;
            overflow-x: scroll;
            width:300px;
            margin-left: 50px;
            border:1px solid #617775;
        }
        #check_node_data div{
            padding-left:20px;
        }
    </style>
@endsection
@section('content')
    <section class="content">
        <div class="row">
            <!-- left column -->
            <div class="col-md-6">
                <!-- general form elements -->
                <div class="box box-primary">
                    <div class="box-header with-border">
                        <h3 class="box-title">用户组权限设置</h3>
                    </div>
                    <!-- /.box-header -->
                    <!-- form start -->
                    <form role="form" action="/admini/adminusergroup/{{$data['id'] or ''}}/set" method="post" id="adminusergroup-form">
                        <div class="form-group datadd-form-show-error has-error" <?php echo !empty($errors->all()) ? 'style="display:block;"' : '';?>><?php echo !empty($errors->all()) ? implode(' ',$errors->all()) : '';?></div>
                        <input type="hidden" name="_token" value="{{csrf_token()}}">
                        <div class="box-body">
                            <div class="form-group" id="check_node_data">
                                <?php echo !empty($data['setNodeData']) ? $data['setNodeData'] : '';?>
                            </div>
                        </div>
                        <!-- /.box-body -->

                        <div class="box-footer">
                            <button class="btn btn-primary" type="submit">设置</button>
                            <a href="/admini/adminusergroup" class="btn bg-olive">返回列表</a>
                        </div>
                    </form>
                </div>
                <!-- /.box -->
            </div>
            <!--/.col (left) -->
        </div>
        <!-- /.row -->
    </section>
@endsection
@section('privatefooter')
    <!-- jquery-validator -->
    <script src="{{__BASE__}}/admin/plugins/jquery-validate/jquery.validate.min.js"></script>
    <script src="{{__BASE__}}/admin/plugins/jquery-validate/localization/messages_zh.min.js"></script>
    <script>
        $(function(){
            //根据浏览器的高度，动态计算权限设置框的高度
            function setnodeHeight()
            {
                var H = parseInt($(window).height()) - 300;
                $("#check_node_data").height(H);
            }
            setnodeHeight();
            //用户权限设置选中
            $("#check_node_data div input[type=checkbox]").on("click", function () {
                var id = $(this).attr('id_n');
                var pid = $(this).attr('pid');
                var ischecked = $(this).prop("checked");
                if(ischecked){
                    trueNode(id,pid);
                }else{
                    falseNode(id,pid);
                }
            });
            function trueNode(id,pid) {
                var thisPid = '';
                var thisID = '';
                $("#check_node_data div input[type=checkbox]").each(function(n){
                    var thisIId = $(this).attr("id_n");
                    var thisPPID = $(this).attr("pid");
                    if(pid == thisIId)
                    {
                        $(this).prop("checked",true);
                        thisPid = $(this).attr("pid");
                    }
                    if(id == thisPPID)
                    {
                        $(this).prop("checked",true);
                        thisID = $(this).attr("id_n");
                    }
                    if(thisPid.length > 0 || thisID.length > 0){
                        trueNode(thisID,thisPid);
                    }
                });
            }

            function falseNode(id,pid) {
                var thisPid = '';
                var thisID = '';
                $("#check_node_data div input[type=checkbox]").each(function(n){
                    var thisIId = $(this).attr("id_n");
                    var thisPPID = $(this).attr("pid");
                    if(pid == thisIId)
                    {
                        var haschecked = 0;
                        $(this).parent("div").siblings("div").children("input[type=checkbox]").each(function(){
                            if(pid == $(this).attr("pid"))
                            {
                                haschecked = parseInt(haschecked) + 1;
                            }
                        });
                        if(haschecked == 0)
                        {
                            $(this).prop("checked",false);
                            thisPid = $(this).attr("pid");
                        }
                    }
                    if(id == thisPPID)
                    {
                        $(this).prop("checked",false);
                        thisID = $(this).attr("id_n");
                    }
                    if(thisPid.length > 0 || thisID.length > 0){
                        falseNode(thisID,thisPid);
                    }
                });
            }

        });
    </script>
@endsection